package com.winit.openapi.ums.controller;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ums.spi.finance.FinanceUserService;
import com.winit.ums.spi.finance.command.CheckAccountIsExistCommand;
import com.winit.ums.spi.finance.command.CheckVerificationCodeCommand;
import com.winit.ums.spi.finance.command.CheckWinitAccountCommand;
import com.winit.ums.spi.finance.command.FinanceUserCommand;
import com.winit.ums.spi.finance.command.ForgetPasswordCommand;
import com.winit.ums.spi.finance.command.LoginFinanceUserCommand;
import com.winit.ums.spi.finance.command.MatchUserAndAccountCommand;
import com.winit.ums.spi.finance.command.ModifPasswordCommand;
import com.winit.ums.spi.finance.command.ModifyFinanceUserCommand;
import com.winit.ums.spi.finance.command.QueryFinanceCommand;
import com.winit.ums.spi.finance.command.QueryFinanceUserTokenCommand;
import com.winit.ums.spi.finance.command.SendVerificationCodeCommand;
import com.winit.ums.spi.finance.vo.FinanceBaseInfoVo;

/**
 * 金融用户Controller
 * @author qibin.tang
 *
 */
@Controller
@RequestMapping(value = "/ums/financeUser")
public class FinanceUserController extends BaseController {

	@Resource
    private FinanceUserService financeUserService;
	
	/**
	 * 金融客户注册
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
    @RequestMapping(value = "/registerFinanceUser")
    public String registerFinanceUser(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        FinanceUserCommand command = JSON.parseObject(requestMsg.getData().toString(),FinanceUserCommand.class);
        command.setCtx(CommandContext.getContext());
        financeUserService.registerFinanceUser(command);
        return SUCCESS;
    }
    
    /**
	 * 获取金融客户TOKEN
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
    @RequestMapping(value = "/getFinanceUserToken")
    public String getFinanceUserToken(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	QueryFinanceUserTokenCommand command = JSON.parseObject(requestMsg.getData().toString(),QueryFinanceUserTokenCommand.class);
    	command.setCtx(CommandContext.getContext());
    	String token = financeUserService.getFinanceUserToken(command);
    	Map<String, Object> returnMapDate = new HashMap<String, Object>();
    	returnMapDate.put("token", token);
    	responseMsg.setData(returnMapDate);
        return SUCCESS;
    }
    
    /**
	 * （金融）发送验证码
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
    @RequestMapping(value = "/sendVerificationCode")
    public String sendVerificationCode(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        SendVerificationCodeCommand command = JSON.parseObject(requestMsg.getData().toString(),SendVerificationCodeCommand.class);
        command.setCtx(CommandContext.getContext());
        financeUserService.sendVerificationCode(command);
        return SUCCESS;
    }
    
    /**
	 * 校验（金融）登陆账号是否存在
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
    @RequestMapping(value = "/checkAccountIsExist")
    public String checkAccountIsExist(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CheckAccountIsExistCommand command = JSON.parseObject(requestMsg.getData().toString(),CheckAccountIsExistCommand.class);
        command.setCtx(CommandContext.getContext());
        boolean result = financeUserService.checkAccountIsExist(command);
        Map<String, Object> returnMapDate = new HashMap<String, Object>();
    	returnMapDate.put("result", result);
    	responseMsg.setData(returnMapDate);
        return SUCCESS;
    }
    
    /**
	 * 校验（金融）验证码
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
    @RequestMapping(value = "/checkVerificationCode")
    public String checkVerificationCode(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	CheckVerificationCodeCommand command = JSON.parseObject(requestMsg.getData().toString(),CheckVerificationCodeCommand.class);
    	command.setCtx(CommandContext.getContext());
        boolean result = financeUserService.checkVerificationCode(command);
        Map<String, Object> returnMapDate = new HashMap<String, Object>();
    	returnMapDate.put("result", result);
    	responseMsg.setData(returnMapDate);
        return SUCCESS;
    }
    
    
    /**
	 * 金融账号登陆
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
    @RequestMapping(value = "/loginForFinanceUser")
    public String loginForFinanceUser(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	LoginFinanceUserCommand command = JSON.parseObject(requestMsg.getData().toString(),LoginFinanceUserCommand.class);
    	command.setCtx(CommandContext.getContext());
    	FinanceBaseInfoVo financeBaseInfoVo = financeUserService.loginForFinanceUser(command);
    	Map<String, Object> returnMapDate = new HashMap<String, Object>();
    	returnMapDate.put("financeBaseInfoVo", financeBaseInfoVo);
    	responseMsg.setData(returnMapDate);
        return SUCCESS;
    }
    
    /**
     * 万邑联免登录跳转到万邑金选网站
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/loginByWinitAccount")
    public String loginForWinitAccountAndToken(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CheckWinitAccountCommand command = JSON.parseObject(requestMsg.getData().toString(),CheckWinitAccountCommand.class);
        command.setCtx(CommandContext.getContext());
        FinanceBaseInfoVo financeBaseInfoVo = financeUserService.checkAndLoginInfo(command);
        Map<String, Object> returnMapDate = new HashMap<String, Object>();
        returnMapDate.put("financeBaseInfoVo", financeBaseInfoVo);
        responseMsg.setData(returnMapDate);
        return SUCCESS;
    }
    
    /**
	 * 查询金融客户信息
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
    @RequestMapping(value = "/queryFinanceCustomer")
    public String queryFinanceCustomer(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	QueryFinanceCommand command = new QueryFinanceCommand();
    	command.setUserName(CommandContext.getContext().getUsername());
    	FinanceBaseInfoVo financeBaseInfoVo = financeUserService.queryFinanceCustomer(command);
    	Map<String, Object> returnMapDate = new HashMap<String, Object>();
    	returnMapDate.put("financeBaseInfoVo", financeBaseInfoVo);
    	responseMsg.setData(returnMapDate);
        return SUCCESS;
    }
    
    /**
	 * 修改登陆账号
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
    @RequestMapping(value = "/modifFinanceAccount")
    public String modifFinanceAccount(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	ModifyFinanceUserCommand command = JSON.parseObject(requestMsg.getData().toString(),ModifyFinanceUserCommand.class);
        command.setCtx(CommandContext.getContext());
        command.setUserName(command.getCtx().getUsername());
        financeUserService.modifyFinanceUser(command);
        return SUCCESS;
    }
    
    /**
	 * 修改密码
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
    @RequestMapping(value = "/modifPassword")
    public String modifPassword(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	ModifPasswordCommand command = JSON.parseObject(requestMsg.getData().toString(),ModifPasswordCommand.class);
    	command.setCtx(CommandContext.getContext());
    	command.setUserName(command.getCtx().getUsername());
    	financeUserService.modifyPasswordForFinanceUser(command);
        return SUCCESS;
    }
    
    /**
	 * 忘记密码
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
    @RequestMapping(value = "/forgetPassword")
    public String forgetPassword(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	ForgetPasswordCommand command = JSON.parseObject(requestMsg.getData().toString(),ForgetPasswordCommand.class);
        command.setCtx(CommandContext.getContext());
        financeUserService.forgetPassword(command);
        return SUCCESS;
    }
    
    /**
     * 关联万邑联账号
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/matchUserAndAccount")
    public String matchUserAndAccount(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
    	MatchUserAndAccountCommand command = JSON.parseObject(requestMsg.getData().toString(),MatchUserAndAccountCommand.class);
    	command.setCtx(CommandContext.getContext());
    	financeUserService.matchUserAndAccount(command);
    	return SUCCESS;
    }
}
